****TABLE 8****

cd "$main_dir/output/tables/main"

*********************************************************************************************************************************************************************
*********************************************************************************************************************************************************************
*COLUMNS 1-3 - GLOBAL SAMPLE********************************************************************************************************************
*********************************************************************************************************************************************************************
*********************************************************************************************************************************************************************

global basic 	sex marital religion edu

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/harmonized_dataset.dta"

merge m:1 ccode survey_year yob using "$data_dir/raw/lifeexp_variables_data/growth_rate_mpd_var_exp.dta", nogen keepusing(total_growth_rate_obs wgrowth_rate_1 yob_ln_gdppc total_py_growth_rate_obs preyob_growth_rate_5 preyob_growth_rate_10 preyob_growth_rate_15)

fegen subregion_yob = group(subregion yob) if yob!=-99
fegen svy_survey_year = group(survey_year survey)

cap drop sample pre_birth_sample
gen sample = 1 if born_country!=0 & total_growth_rate_obs>=0.9*(age-1) & total_growth_rate_obs!=.
qui reghdfe trust_govt wgrowth_rate_1 yob_ln_gdppc if sample==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
gen pre_birth_sample = 1 if e(sample)==1

//--------------------------------------------------------
//Create Table 8 (Columns 1-3)
//--------------------------------------------------------

//Column 1
qui reghdfe trust_govt preyob_growth_rate_5 yob_ln_gdppc if pre_birth_sample==1 & total_py_growth_rate_obs>=5 & total_py_growth_rate_obs!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
local list trust_govt preyob_growth_rate_5 yob_ln_gdppc
cap drop z*
foreach x of local list {
	egen z`x' = std(`x') if e(sample)==1
}
reghdfe ztrust_govt zpreyob_growth_rate_5 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_8.doc, replace dec(3) nocons addtext(Geographic Unit FE, Country, ///
													    Sample, Harmonized) keep(zpreyob_growth_rate_5) 

//Column 2														
qui reghdfe trust_govt preyob_growth_rate_10 yob_ln_gdppc if pre_birth_sample==1 & total_py_growth_rate_obs>=10 & total_py_growth_rate_obs!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
local list trust_govt preyob_growth_rate_10 yob_ln_gdppc
cap drop z*
foreach x of local list {
	egen z`x' = std(`x') if e(sample)==1
}
reghdfe ztrust_govt zpreyob_growth_rate_10 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_8.doc, append dec(3) nocons addtext(Geographic Unit FE, Country, ///
													    Sample, Harmonized) keep(zpreyob_growth_rate_10) 
	
//Column 3	
qui reghdfe trust_govt preyob_growth_rate_15 yob_ln_gdppc if pre_birth_sample==1 & total_py_growth_rate_obs>=15 & total_py_growth_rate_obs!=., absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
local list trust_govt preyob_growth_rate_15 yob_ln_gdppc
cap drop z*
foreach x of local list {
	egen z`x' = std(`x') if e(sample)==1
}
reghdfe ztrust_govt zpreyob_growth_rate_15 zyob_ln_gdppc if e(sample)==1, absorb(ccode subregion_yob svy_survey_year age $basic) cluster(ccode)
outreg2 using table_8.doc, append dec(3) nocons addtext(Geographic Unit FE, Country, ///
													    Sample, Harmonized) keep(zpreyob_growth_rate_15)	
														

*****************************************************************************************************************************************************************
*****************************************************************************************************************************************************************
*COLUMNS 4-6 - US SAMPLE (ANES)**********************************************************************************************************************************
*****************************************************************************************************************************************************************
*****************************************************************************************************************************************************************

global basic 	sex marital religion edu ethnicity birth_state
global extra	employment_status occupation income urban
global time 	survey_year age generation
global polit    party partisan

//--------------------------------------------------------
//Read in relevant datasets
//--------------------------------------------------------

clear
use "$data_dir/clean/anes_final.dta"

egen scode_survey_year = group(state_code survey_year)

//--------------------------------------------------------
//Create Table 8 (Column 4-6)
//--------------------------------------------------------														

//Columns 4-6
local varlist preyob_growth_rate_5 preyob_growth_rate_10 preyob_growth_rate_15
foreach var of local varlist {
	
	qui reghdfe trust_fed_govt `var' yob_ln_gdppc, absorb(state_code $time $basic $extra $polit) cluster(yob)
	
	local list trust_fed_govt `var' yob_ln_gdppc
	cap drop z*
	foreach x of local list {
		egen z`x' = std(`x') if e(sample)==1
	}
	
	reghdfe ztrust_fed_govt z`var' zyob_ln_gdppc, absorb(state_code $time $basic $extra $polit) cluster(yob)
	outreg2 using table_8.doc, append dec(3) nocons addtext(Geographic Unit FE, State, ///
													    Sample, ANES) keep(z`var') 								
}
